﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="NhomQuyenByToChuc.ascx.cs"
    Inherits="Viewer.PhanQuyen.UserControls.NhomQuyenByToChuc" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<%@ Register Src="QUNguoiDung.ascx" TagName="QUNguoiDung" TagPrefix="uc1" %>
<link href="../../resources/css/main.css" rel="stylesheet" type="text/css" />
<ext:XScript runat="server">
    <script type="text/javascript">
        function grdNguoiDung_Command(command, record) {
            if (command == 'Delete') { 
                Ext.MessageBox.show({
                   title: 'Confirm',
                   msg: 'Bạn có chắc chắn xóa record này ko?',
                   icon: Ext.MessageBox.QUESTION,
                   buttons: Ext.MessageBox.YESNO,
                   fn: function(btn)
                   {
                    if(btn.toLowerCase()=='yes')
                    {
                        //Do Yes
                        #{storeNguoiDung_NhomQuyenByToChuc}.remove(record);
                        #{storeNguoiDung}.add(record);
                        #{storeNguoiDung}.sort('Name', 'ASC');
                       
                    }
                   }
                });
            }
        }
        
        function GetNguoiDungIds()
        {
            var msg = [];
            var records =#{grdNguoiDung_NhomQuyenByToChuc}.store.getRange();
            Ext.each(records, function (record) {
                msg.push(record.id);
		    });
		    return msg.join(",");
        }
        
        function NodeSelected(){
            if(#{__DialogType}.getValue()=='Detail')
            {
                 #{btnEditNode}.disable();
                 return;
            }
            var node=#{treeToChuc}.getSelectionModel().getSelectedNode();
            if(node.parentNode.text=='ToChuc'){
                #{btnEditNode}.enable();
                return ;
            }
           if(eval(node.parentNode.attributes.IsModuleConThuaKe)){
                #{btnEditNode}.disable();
            }
            else{
                #{btnEditNode}.enable();    
            }          
        }
        
      function ShowEditNodeDialog() {
        var node=#{treeToChuc}.getSelectionModel().getSelectedNode();
        #{lblTenToChuc}.setText(node.attributes.TenToChuc);
        #{chkIsView}.setValue(node.attributes.IsView);
        #{chkIsAdd}.setValue(node.attributes.IsAdd);
        #{chkIsEdit}.setValue(node.attributes.IsEdit);
        #{chkIsDelete}.setValue(node.attributes.IsDelete);
        #{chkIsModuleConThuaKe}.setValue(node.attributes.IsModuleConThuaKe);
        
        if (node.attributes.IsQuyenView=='true')
        {
            #{chkIsView}.enable();
        }else #{chkIsView}.disable();
        if (node.attributes.IsQuyenAdd=='true')
        {
            #{chkIsAdd}.enable();
        }else #{chkIsAdd}.disable();
        if (node.attributes.IsQuyenEdit=='true')
        {
            #{chkIsEdit}.enable();
        }else #{chkIsEdit}.disable();
        if (node.attributes.IsQuyenDelete =='true')
        {
            #{chkIsDelete}.enable();
        }else #{chkIsDelete}.disable();
        
        
        
            #{wdEditNode}.show();
        }
        
        function UpdatedNode()
        {
            var currentNode=#{treeToChuc}.getSelectionModel().getSelectedNode();
            SyncNode(currentNode,#{chkIsView}.getValue(),#{chkIsAdd}.getValue(),#{chkIsEdit}.getValue(),#{chkIsDelete}.getValue(),#{chkIsModuleConThuaKe}.getValue());
            #{wdEditNode}.hide();
        }
        
         function SyncNode(node,IsView,IsAdd,IsEdit,IsDelete,IsModuleConThuaKe)
        {
            //update data
            node.attributes.IsView=IsView;
            node.attributes.IsAdd=IsAdd;
            node.attributes.IsEdit=IsEdit;
            node.attributes.IsDelete=IsDelete;
            node.attributes.IsModuleConThuaKe=IsModuleConThuaKe;
            //Sync GUI
            SyncGUI(node);
            //
            if(!IsModuleConThuaKe)
            {
                return ;
            }
            
            if (!node.hasChildNodes()){
                return ;
            }            
            node.expand();
            //sync children
            var i = 0;
            for(i = 0 ; i < node.childNodes.length ; i++)
            {   //Sync Column
                SyncNode(node.childNodes[i],IsView,IsAdd,IsEdit,IsDelete,IsModuleConThuaKe);
            }
        }
        
         function SyncGUI(node){
           var nodeEl = Ext.get(#{treeToChuc}.getNodeById(node.id).ui.elNode),
                columns = nodeEl.select(".x-treegrid-text", true),
                columnIsView = columns.item(0),
                columnIsAdd = columns.item(1);
                columnIsEdit = columns.item(2);
                columnIsDelete = columns.item(3);
                columnIsModuleConThuaKe = columns.item(4);
                
               UpdateColumn(columnIsView,node.attributes.IsView);
               UpdateColumn(columnIsAdd,node.attributes.IsAdd);
               UpdateColumn(columnIsEdit,node.attributes.IsEdit);
               UpdateColumn(columnIsDelete,node.attributes.IsDelete);
               UpdateColumn(columnIsModuleConThuaKe,node.attributes.IsModuleConThuaKe);
        }
        
        function UpdateColumn(column,checked){
            if(checked){
                column.update("<div class='x-grid3-check-col-on x-grid3-cc-booleanCol'>&nbsp;</div>");
            }
            else {
                column.update("<div class='x-grid3-check-col x-grid3-cc-booleanCol'>&nbsp;</div>");
            }
        }
        
        var refreshTree = function (tree,result) {            
            var nodes = eval(result);
            if(nodes.length > 0){
                tree.initChildren(nodes);
            }
            else{
                tree.getRootNode().removeChildren();
            }
        }
        
          var prepare = function (grid, toolbar, rowIndex, record) {
            var firstButton = toolbar.items.get(0);           
            if(#{__DialogType}.getValue()=='Detail') {
                firstButton.setDisabled(true);
            }
        }    
        
        function checkAll()
        {
            #{chkIsDelete}.setValue(!(#{chkIsDelete}.disabled) && #{chkAll}.checked);
            #{chkIsView}.setValue(!(#{chkIsView}.disabled) && #{chkAll}.checked);
            #{chkIsEdit}.setValue(!(#{chkIsEdit}.disabled) && #{chkAll}.checked);
            #{chkIsAdd}.setValue(!(#{chkIsAdd}.disabled) && #{chkAll}.checked);
            #{chkIsModuleConThuaKe}.setValue(!(#{chkIsModuleConThuaKe}.disabled) && #{chkAll}.checked);
        }
    </script>
</ext:XScript>
<%--Window AddNew--%>
<ext:Window ID="wdAddNew" runat="server" Width="570" AutoHeight="true" Modal="true"
    Resizable="false" Border="false" Hidden="true">
    <Items>
        <ext:Panel ID="pnlNhomQuyenByToChuc" runat="server" Border="false">
            <Content>
                <div style="width: 100%; padding: 5px;">
                    <table width="380">
                        <tr>
                            <td width="170">
                                <b>Tên Nhóm Quyền (Tổ Chức)</b>
                            </td>
                            <td>
                                <ext:Label ID="lblTenNhomQuyenByToChuc" runat="server">
                                </ext:Label>
                                <ext:TextField ID="txtTenNhomQuyenByToChuc" runat="server" Width="200" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>Người Dùng Chịu Trách Nhiệm</b>
                            </td>
                            <td>
                                <ext:TextField ID="txtTenUserOwner" runat="server" Width="200" Disabled="true" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <b>Ghi Chú</b>
                            </td>
                            <td>
                                <ext:Label ID="lblGhiChu" runat="server">
                                </ext:Label>
                                <ext:TextField ID="txtGhiChu" runat="server" Width="200" />
                            </td>
                        </tr>
                    </table>
                </div>
            </Content>
        </ext:Panel>
        <ext:TabPanel ID="pnlPhanQuyen" runat="server" Height="400">
            <Items>
                <ext:Panel ID="pnlNguoiDung" runat="server" Title="Người Dùng" Layout="Fit" MonitorResize="true"
                    Icon="User">
                    <Items>
                        <ext:GridPanel ID="grdNguoiDung_NhomQuyenByToChuc" runat="server" AutoExpandColumn="TenDangNhap"
                            EnableColumnMove="false">
                            <Store>
                                <ext:Store ID="storeNguoiDung_NhomQuyenByToChuc" runat="server">
                                    <Reader>
                                        <ext:JsonReader IDProperty="NguoiDungID">
                                            <Fields>
                                                <ext:RecordField Name="TenDangNhap" />
                                            </Fields>
                                        </ext:JsonReader>
                                    </Reader>
                                </ext:Store>
                            </Store>
                            <ColumnModel>
                                <Columns>
                                    <ext:RowNumbererColumn />
                                    <ext:Column Header="Tên Đăng Nhập" DataIndex="TenDangNhap" />
                                    <ext:CommandColumn Width="50">
                                        <Commands>
                                            <ext:GridCommand Icon="Delete" CommandName="Delete" Text="Xóa">
                                            </ext:GridCommand>
                                        </Commands>
                                        <PrepareToolbar Fn="prepare" />
                                    </ext:CommandColumn>
                                </Columns>
                            </ColumnModel>
                            <TopBar>
                                <ext:Toolbar ID="Toolbar1" runat="server">
                                    <Items>
                                        <ext:Button runat="server" Icon="UserAdd" ID="btnShowDialogAddNguoiDung" Text="Thêm Người Dùng">
                                            <Listeners>
                                                <Click Handler="#{wdQUNguoiDung}.show();"></Click>
                                            </Listeners>
                                        </ext:Button>
                                        <%--  <ext:Button runat="server" ID="Button1" Text="Tải dữ liệu" Icon="Reload">
                                            <Listeners>
                                                 <Click Handler="#{DirectMethods}.ReloadData();"></Click>
                                            </Listeners>
                                        </ext:Button>--%>
                                    </Items>
                                </ext:Toolbar>
                            </TopBar>
                            <%--<BottomBar>
                                <ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="10" PageIndex="1" />
                            </BottomBar>--%>
                            <Listeners>
                                <Command Handler="grdNguoiDung_Command(command,record);" />
                            </Listeners>
                        </ext:GridPanel>
                        <ext:Hidden ID="__NguoiDungIds" runat="server">
                        </ext:Hidden>
                    </Items>
                </ext:Panel>
                <ext:Panel ID="tabToChuc" runat="server" Title="Tổ Chức" Layout="Fit" MonitorResize="true"
                    Icon="ChartOrganisation">
                    <Items>
                        <ext:TreeGrid ID="treeToChuc" runat="server" OnSubmit="SubmitNodes" Collapsed="false"
                            Height="300" NoLeafIcon="true" EnableHdMenu="false" EnableSort="false" EnableDD="false">
                            <TopBar>
                                <ext:Toolbar ID="Toolbar2" runat="server">
                                    <Items>
                                        <ext:Button ID="btnEditNode" Icon="TabEdit" runat="server" Text="Chỉnh sửa" Disabled="true">
                                            <Listeners>
                                                <Click Handler="ShowEditNodeDialog();" />
                                            </Listeners>
                                        </ext:Button>
                                    </Items>
                                </ext:Toolbar>
                            </TopBar>
                            <SelectionModel>
                                <ext:DefaultSelectionModel ID="DefaultSelectionModel1" runat="server">
                                    <Listeners>
                                        <SelectionChange Handler="NodeSelected(); " />
                                    </Listeners>
                                </ext:DefaultSelectionModel>
                            </SelectionModel>
                            <Columns>
                                <ext:TreeGridColumn Header="Tên Tổ Chức" Width="230" DataIndex="TenToChuc" />
                                <ext:TreeGridColumn Header="Xem" Width="60" DataIndex="IsView" Align="Center">
                                    <XTemplate ID="XTemplate1" runat="server">
                                        <Html>
                                        <tpl for=".">
                                             <tpl if="IsView == 'true'"><div class="x-grid3-check-col-on x-grid3-cc-booleanCol">&nbsp;</div></tpl>
                                             <tpl if="IsView == 'false'"><div class="x-grid3-check-col x-grid3-cc-booleanCol">&nbsp;</div></tpl>
                                        </tpl>
                                        </Html>
                                    </XTemplate>
                                </ext:TreeGridColumn>
                                <ext:TreeGridColumn Header="Thêm" Width="60" DataIndex="IsAdd" Align="Center">
                                    <XTemplate ID="XTemplate2" runat="server">
                                        <Html>
                                        <tpl for=".">
                                             <tpl if="IsAdd == 'true' "><div class="x-grid3-check-col-on x-grid3-cc-booleanCol">&nbsp;</div></tpl>
                                             <tpl if="IsAdd == 'false' "><div class="x-grid3-check-col x-grid3-cc-booleanCol">&nbsp;</div></tpl>
                                        </tpl>
                                        </Html>
                                    </XTemplate>
                                </ext:TreeGridColumn>
                                <ext:TreeGridColumn Header="Sửa" Width="60" DataIndex="IsEdit" Align="Center">
                                    <XTemplate ID="XTemplate3" runat="server">
                                        <Html>
                                        <tpl for=".">
                                             <tpl if="IsEdit == 'true'"><div class="x-grid3-check-col-on x-grid3-cc-booleanCol">&nbsp;</div></tpl>
                                             <tpl if="IsEdit == 'false'"><div class="x-grid3-check-col x-grid3-cc-booleanCol">&nbsp;</div></tpl>
                                        </tpl>
                                        </Html>
                                    </XTemplate>
                                </ext:TreeGridColumn>
                                <ext:TreeGridColumn Header="Xóa" Width="60" DataIndex="IsDelete" Align="Center">
                                    <XTemplate ID="XTemplate4" runat="server">
                                        <Html>
                                        <tpl for=".">
                                             <tpl if="IsDelete == 'true' "><div class="x-grid3-check-col-on x-grid3-cc-booleanCol">&nbsp;</div></tpl>
                                             <tpl if="IsDelete == 'false' "><div class="x-grid3-check-col x-grid3-cc-booleanCol">&nbsp;</div></tpl>
                                        </tpl>
                                        </Html>
                                    </XTemplate>
                                </ext:TreeGridColumn>
                                <ext:TreeGridColumn Header="Thừa Kế" Width="60" DataIndex="IsModuleConThuaKe" Align="Center">
                                    <XTemplate ID="XTemplate5" runat="server">
                                        <Html>
                                        <tpl for=".">
                                             <tpl if="IsModuleConThuaKe == 'true' "><div class="x-grid3-check-col-on x-grid3-cc-booleanCol">&nbsp;</div></tpl>
                                             <tpl if="IsModuleConThuaKe == 'false' "><div class="x-grid3-check-col x-grid3-cc-booleanCol">&nbsp;</div></tpl>
                                        </tpl>
                                        </Html>
                                    </XTemplate>
                                </ext:TreeGridColumn>
                            </Columns>
                            <Root>
                            </Root>
                        </ext:TreeGrid>
                    </Items>
                </ext:Panel>
            </Items>
        </ext:TabPanel>
    </Items>
    <Listeners>
        <BeforeShow Handler="#{btnEditNode}.disable();" />
    </Listeners>
    <Buttons>
        <ext:Button ID="btnSave" runat="server" Text="Lưu" Icon="Disk">
            <Listeners>
                <Click Handler="
                                                if(#{txtTenNhomQuyenByToChuc}.getValue().trim()=='')
                                                {
                                                    Ext.MessageBox.alert('Message', 'Xin vui lòng nhập Tên Nhóm Quyền !');
                                                    #{txtTenNhomQuyenByToChuc}.focus();
                                                    return false;
                                                }
                                                #{__NguoiDungIds}.setValue(GetNguoiDungIds());
                                                #{treeToChuc}.submitNodes();
                                             " />
            </Listeners>
        </ext:Button>
        <ext:Button ID="btnClose" runat="server" Text="Đóng" Icon="Cancel">
            <Listeners>
                <Click Handler="#{wdAddNew}.hide();" />
            </Listeners>
        </ext:Button>
    </Buttons>
</ext:Window>
<uc1:QUNguoiDung ID="ucQUNguoiDung" runat="server" />
<ext:Hidden ID="__DialogType" runat="server">
</ext:Hidden>
<ext:Hidden ID="__NhomQuyenByToChucId" runat="server">
</ext:Hidden>
<%--Window edit :NhomQuyenByToChuc-ToChuc--%>
<ext:Window ID="wdEditNode" Title="Chỉnh sửa" runat="server" Width="480" AutoHeight="true"
    Icon="TabEdit" Modal="true" Hidden="true">
    <Content>
        <div align="center">
            <br />
            <table style="width: 90%; text-align: center;" cellpadding="10" cellspacing="0">
                <tr>
                    <td colspan="5">
                        <h2>
                            <ext:Label ID="lblTenToChuc" runat="server">
                            </ext:Label>
                        </h2>
                        <br />
                    </td>
                </tr>
                <tr>
                    <td>
                        Xem
                    </td>
                    <td>
                        Thêm
                    </td>
                    <td>
                        Sửa
                    </td>
                    <td>
                        Xóa
                    </td>
                    <td style="border-left: solid 1px;">
                        Thừa Kế
                    </td>
                    <td style="border-left: solid 1px;">
                        Chọn Tất Cả
                    </td>
                </tr>
                <tr>
                    <td>
                        <ext:Checkbox ID="chkIsView" runat="server" />
                    </td>
                    <td>
                        <ext:Checkbox ID="chkIsAdd" runat="server" />
                    </td>
                    <td>
                        <ext:Checkbox ID="chkIsEdit" runat="server" />
                    </td>
                    <td>
                        <ext:Checkbox ID="chkIsDelete" runat="server" />
                    </td>
                    <td style="border-left: solid 1px;">
                        <ext:Checkbox ID="chkIsModuleConThuaKe" runat="server" />
                    </td>
                    <td style="border-left: solid 1px;">
                        <ext:Checkbox ID="chkAll" runat="server" >
                            <Listeners>
                                <Check Handler="checkAll();"/>
                            </Listeners>
                        </ext:Checkbox>
                    </td>
                </tr>
            </table>
            <br />
        </div>
    </Content>
    <Buttons>
        <ext:Button ID="btnOk" runat="server" Icon="Accept" Text="Lưu" Width="60">
            <Listeners>
                <Click Handler="UpdatedNode(); " />
            </Listeners>
        </ext:Button>
        <ext:Button ID="btnCloseRole" runat="server" Icon="Cancel" Text="Hủy">
            <Listeners>
                <Click Handler="#{wdEditNode}.hide();" />
            </Listeners>
        </ext:Button>
    </Buttons>
</ext:Window>
